package com.macro.mall.portal.service.impl;

import cn.hutool.core.date.DateUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.MbrMemberHistoryMapper;
import com.macro.mall.model.MbrMemberHistory;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.MbrMemberHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * MbrMemberHistory的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class MbrMemberHistoryServiceImpl implements MbrMemberHistoryService {

    private boolean isDebugEnabled;

    @Autowired
    private MbrMemberHistoryMapper mbrMemberHistoryMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public Map<String, Object> find(MbrMemberHistory value) {
        //TODO这里可以做通过Assist做添加查询
        Assist assist = new Assist();
        Long userId = null;
        List<MbrMemberHistory> result = null;
        Map<String, Object> result1 = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
            assist.andEq("member_id", userId);
            assist.setOrder(Assist.order("create_time", false));
            result = mbrMemberHistoryMapper.selectMbrMemberHistory(assist);
            result1 = buildResult(result);


        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取MbrMemberHistory数据集-->结果:", result);
        }
        return result1;
    }

    private Map<String, Object> buildResult(List<MbrMemberHistory> result) {
        Map<String, Object> map = new HashMap<>();
        List<MbrMemberHistory> l1 = new ArrayList<>();
        List<MbrMemberHistory> l2 = new ArrayList<>();
        List<MbrMemberHistory> l3 = new ArrayList<>();
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        Date time = calendar.getTime();
        for (MbrMemberHistory data : result) {
            if (DateUtil.isSameDay(date, data.getCreateTime())) {
                l1.add(data);
            } else if (DateUtil.isSameDay(time, data.getCreateTime())) {
                l2.add(data);
            } else {
                l3.add(data);
            }
        }
        map.put("today",l1);
        map.put("yesterday",l2);
        map.put("earlier",l3);
        return map;
    }

    @Override
    public MbrMemberHistory findOne(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberHistory的id获得MbrMemberHistory对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberHistory的id获得MbrMemberHistory对象-->失败:id不能为空");
        }
        MbrMemberHistory result = null;
        try {
            result = mbrMemberHistoryMapper.selectMbrMemberHistoryById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberHistory的id获得MbrMemberHistory对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(Integer value) {
        MbrMemberHistory mbrMemberHistory = new MbrMemberHistory();
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将MbrMemberHistory中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        Assist assist = new Assist();
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            Long userId = memberDetails.getUmsMember().getId();
            mbrMemberHistory.setMemberId(userId);
            mbrMemberHistory.setCourseId(Long.parseLong(value + ""));
            userId = memberDetails.getUmsMember().getId();
            assist.andEq("member_id", userId);
            assist.andEq("course_id", value);
            long mbrMemberHistoryRowCount = mbrMemberHistoryMapper.getMbrMemberHistoryRowCount(assist);
            if (mbrMemberHistoryRowCount == 0) {
                result = mbrMemberHistoryMapper.insertNotNullMbrMemberHistory(mbrMemberHistory);
            } else {
                Assist assist1 = new Assist();
                assist1.andEq("member_Id",userId);
                assist1.andEq("course_Id",value);
                mbrMemberHistoryMapper.updateNotNullMbrMemberHistory(MbrMemberHistory.builder().createTime(new Date()).build(),assist1);
            }
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将MbrMemberHistory中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(MbrMemberHistory value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberHistory的id更新MbrMemberHistory中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过MbrMemberHistory的id更新MbrMemberHistory中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = mbrMemberHistoryMapper.updateNotNullMbrMemberHistoryById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberHistory的id更新MbrMemberHistory中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(String id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberHistory的id删除MbrMemberHistory-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberHistory的id删除MbrMemberHistory-->失败:id不能为空");
        }
        int result = 0;
        try {
            String[] split = id.split(",");
            mbrMemberHistoryMapper.deleteMbrMemberHistoryById(split);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberHistory的id删除MbrMemberHistory-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public long count(MbrMemberHistory mbrMemberHistory) {
        //TODO这里可以做通过Assist做添加查询
        Assist assist = new Assist();
        Long userId = null;
        List<MbrMemberHistory> result = null;
        long mbrMemberHistoryRowCount = 0;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
            assist.andEq("member_id", userId);
            mbrMemberHistoryRowCount = mbrMemberHistoryMapper.getMbrMemberHistoryRowCount(assist);

        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取MbrMemberHistory数据集-->结果:", result);
        }
        return mbrMemberHistoryRowCount;
    }


}